<template>
    <TeacherDetail :teacher="teacher" @submit="handleSubmit" />
</template>

<script setup>
import TeacherDetail from '../components/TeacherDetail.vue';
import { useMemberStore } from '../stores/member';
import { useUserStore } from '../stores/user';
import { ref, onMounted } from 'vue';
import { ElMessage } from 'element-plus';

const userStore = useUserStore()
const memberStore = useMemberStore()

const teacher = ref({
    id:null,
    name:'',
    subjects:[]
})

//const teacher = computed(() =>{
    //return memberStore.getUserByUsername(userStore.username)
//})

onMounted(() => {
    const t = memberStore.getUserByUsername(userStore.username)
    if (t) {
        teacher.value.id = t.id
        teacher.value.name = t.name
        teacher.value.subjects = t.subjects
    }
})

const handleSubmit = () =>{
    memberStore.updateTeacherSubjects(teacher.value.id, teacher.value.subjects)
    ElMessage.success('任教科目已保存')
}
</script>